package com.ken.elmboot.service.impl;

import com.ken.elmboot.bean.Food;
import com.ken.elmboot.common.ElmException;
import com.ken.elmboot.mapper.FoodMapper;
import com.ken.elmboot.service.FoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author ken
 */
@Service
@Transactional(rollbackFor = ElmException.class)
public class FoodServiceImpl extends DefaultServiceImpl<Food> implements FoodService {

    @Autowired
    private FoodMapper foodMapper;

    @Override
    public boolean add(Food object) {
        boolean ret = false;
        try {
            ret = foodMapper.add(object);
        }catch (Exception e) {
            e.printStackTrace();
            throw new ElmException("增加食品异常",e);
        }
        return ret;
    }

    @Override
    public boolean deleteById(Integer id) {
        boolean ret = false;
        try {
            ret = foodMapper.deleteById(id);
        }catch (Exception e) {
            e.printStackTrace();
            throw new ElmException("删除食品异常",e);
        }
        return ret;
    }

    @Override
    public boolean edit(Food object) {
        boolean ret = false;
        try {
            ret = foodMapper.edit(object);
        }catch (Exception e) {
            e.printStackTrace();
            throw new ElmException("修改食品异常",e);
        }
        return ret;
    }

    @Override
    public boolean deleteByIds(List<Integer> ids) {
        boolean ret = false;
        try {
            for (int id : ids) {
                foodMapper.deleteById(id);
            }
        }catch (Exception e) {
            e.printStackTrace();
            throw new ElmException("删除食品异常",e);
        }
        return ret;
    }
}
